<?php
import("Com.Auth");
import('ORG.Util.Page'); //导入分页类
import('ORG.Net.UploadFile');
import('ORG.Io.Dir');
class UserAction extends Action
{
	public function _initialize(){
		$this->assign('no',Auth::getno());
    	$this->assign('name',Auth::getname());
    	$this->assign('role',Auth::getrole());
	}
	public function menu(){	//菜单
		$this->assign('role',Auth::getrole()); // 模板发量赋值
		$this->display(); // 输出模板
	}
	public function index(){//主页
		$this->display();
	}
//院系
	public function collegedetail(){//院系管理
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
    		if(Auth::getrole()!='校级管理员')
    		{
    			$map['id']=D('info')->where('no='.Auth::getno())->getfield('departmentid');
				$Department=D("Department");// 实例化模型类
				$count = $Department->where($map)->count();    //计算总数 
	    		$p = new Page ( $count, 20 ); 
				$list=$Department->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
    		} 
    		else 
    		{
				$Department=D("Department");// 实例化模型类
				$count = $Department->count();    //计算总数 
			    $p = new Page ( $count, 20 ); 
				$list=$Department->limit($p->firstRow.','.$p->listRows)->order('id')->select();
				$a=1;
				$this->assign ( "a", $a );
    		}
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('list',$list); // 模板发量赋值
		$this->display(); // 输出模板
	}
	}
	public function collegeadd(){//院系添加
		if(Auth::getrole()!='校级管理员')
			$this->error('权限不够',1);
		else{
			$Department = D("Department"); // 实例化模型类
			$_POST['course']=0;
			$_POST['people']=0;
			if($Department->Create()){// 创建数据对象
				if($Department->add()){ // 写入数据库
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}
				else{
					$this->error('数据库写入错误',1);
				}
			}else{
				$this->error($Department->getError(),1);
			}

		}
    } 
	public  function collegedelete(){//院系删除
		if(Auth::getrole()!='校级管理员')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$department=D("department");
			$department->delete($id);
			$this->redirect('collegedetail');
		}
    } 
	public  function collegeedit(){//院系编辑
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			if (Auth::getrole()=='校级管理员')
			{
				$a=1;
				$this->assign('a',$a);
			}
			$c=D("custom1_detail")->getByDepartmentid($_GET['id']);
			$this->assign('c',$c);
            $department=D("department"); 
            $vo=$department->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
	public function collegeupdate() { //院系更新
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
        $Department=D("Department"); 
		if($Department->Create()){// 创建数据对象
			$Department->save();
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($Department->getError(),1);
		}
    } 
    //系
	public function majordetail(){//院系管理
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
		$major=D("major");// 实例化模型类
		$count = $major->count();    //计算总数 
	    $p = new Page ( $count, 20 ); 
		$list=$major->limit($p->firstRow.','.$p->listRows)->order('id')->select();
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('list',$list); // 模板发量赋值
		$this->display(); // 输出模板
	}
	}
	public function majoradd(){//院系添加
		if(Auth::getrole()!='校级管理员')
			$this->redirect('auth/logon');
		else{
			$major = D("major"); // 实例化模型类
			$_POST['course']=0;
			if($major->Create()){// 创建数据对象
				if($major->add()){ // 写入数据库
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}
				else{
					$this->error('数据库写入错误',1);
				}
			}else{
				$this->error($major->getError(),1);
			}

		}
    } 
	public  function majordelete(){//院系删除
		if(Auth::getrole()!='校级管理员')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$major=D("major");
			$major->delete($id);
			$this->redirect('majordetail');
		}
    } 
	public  function majoredit(){//院系编辑
		if(Auth::getrole()!='校级管理员')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			$c=D("custom1_detail")->getBymajorid($_GET['id']);
			$this->assign('c',$c);
            $major=D("major"); 
            $vo=$major->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
	public function majorupdate() { //院系更新
		if(Auth::getrole()!='校级管理员')
    		$this->redirect('auth/logon');
        $major=D("major"); 
		if($major->Create()){// 创建数据对象
			$major->save();
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($major->getError(),1);
		}
    } 
	//信息
	public  function  infodetail(){//信息管理
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
    		if (Auth::getrole()!='校级管理员')
    			$deplist=D("department")->where('id='.Auth::getdepartment())->select();
    		else
				$deplist=D("department")->select();
    	$this->assign('deplist',$deplist);	
    	//$no=$_GET['no'];
		//$name=$_GET['name'];
		//$dname=$_GET['dname'];
		$Model = D("info_detail");
		if(!empty($_GET['no'])||($_GET['name'])||($_GET['dname'])){
			$map['no'] = array('like','%'.$_GET['no'].'%');
			$map['name'] = array('like','%'.$_GET['name'].'%');
			if ($_GET['dname'])
			$map['dname'] = array('eq',$_GET['dname']);
			if (Auth::getrole()!='校级管理员')
			{
				$map['dname']=array('eq',Auth::getdepartmentname());
				$map['role']=array('neq','校级管理员');
			}
			$count = $Model->where($map)->count();    //计算总数 
	        $p = new Page ( $count, 20 ); 
	    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('no')->select();
    	}
		else {
			if (Auth::getrole()!='校级管理员')
			{
				$map['dname']=array('eq',Auth::getdepartmentname());
				$map['role']=array('neq','校级管理员');
			}
			$count = $Model->where($map)->count();    //计算总数 
	        $p = new Page ( $count, 20 ); 
			$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('no')->select();			
		}
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('list',$list); // 模板发量赋值
		$this->assign('role',Auth::getrole());//传值到前台，做权限判断
		$this->display(); // 输出模板
    }
	}
    public function infoadd(){//信息添加_显示
    	if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
    	if(Auth::getrole()=='校级管理员'){
    		$rolelist=array("学生","教师","院系管理员","校级管理员");
    		$deplist=D("department")->select();
    	}
    	else {
			$rolelist=array("院系管理员","教师","学生");
	   		$deplist=D("department")->where('id='.Auth::getdepartment())->select();
    	}
	    $this->assign('rolelist',$rolelist);
	    $this->assign('deplist',$deplist);
		$this->display();
    	}
    } 
    public function infoadd_post(){//信息添加_更新
    	if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	$info = D("info"); // 实例化模型类
    	if($info->find($_POST['no'])&&$_POST['no'])
    		$this->error('学/工号已存在',1);
    	$data=$info->Create();
		if($data){// 创建数据对象
			$info->add(); // 写入数据库
			$c=M('info');
			$map['departmentid']=$_POST['departmentid'];
			$map['role']=array('neq','学生');
			$count=$c->where($map)->count();
			D('department')->where('id='.$_POST['departmentid'])->setfield('people',$count);
			$this->ajaxReturn($data,'表单数据保存成功！',1);
		}else{
			$this->error($info->getError(),1);
		}
    } 
   public  function infodelete(){//信息删除
   	if (Auth::getrole()=='校级管理员'||Auth::getrole()=='院系管理员')
   	{
		$no=$_POST['no'];
		$info=D("info");
		$info->delete($no);
		$this->redirect('infodetail');
   	}
		else 
			$this->redirect('auth/logon');
    } 
    public  function infoedit(){//信息编辑
    	$dname=D("department")->where('id='.Auth::getdepartment())->getField('name');
    	$info=D('info');
    	$role=$info->where('no='.$_GET["no"])->getfield('role');
    	if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
    	if(Auth::getrole()=='校级管理员'){
    	$rolelist=array("学生","教师","院系管理员","校级管理员");
    	$deplist=D("department")->select();}
    	else{
    		if ($role=='校级管理员')
    			$this->redirect('auth/logon');
    		else {
				$rolelist=array("院系管理员","教师","学生");
				$deplist=D("department")->where('id='.Auth::getdepartment())->select();
    		}
    		}
    	}
    	//$deplist=D("department")->select();
    	$this->assign('rolelist',$rolelist);
    	$this->assign('deplist',$deplist);	
    	$this->assign('dname',$dname);
		if(!empty($_GET['no'])) { 
            $info=D("info"); 
            $vo=$info->getByno($_GET['no']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
    }
    public function infoupdate() { //信息更新
    	if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	$info=D("info");
    	$info->where('no='.$_POST["no"])->setfield('name',$_POST['name']);
       	$info->where('no='.$_POST["no"])->setfield('cno',$_POST['cno']);
    	$info->where('no='.$_POST["no"])->setfield('role',$_POST['role']);
    	$info->where('no='.$_POST["no"])->setfield('departmentid',$_POST['departmentid']);
    	$info->save(); 
        $this->redirect('infodetail');        
    } 
    public function infoimport(){//批量导入
    	if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
    	if(Auth::getrole()=='校级管理员'){
    		$rolelist=array("学生","教师","院系管理员","校级管理员");
    		$deplist=D("department")->select();
    	}
    	else {
			$rolelist=array("学生","教师","院系管理员");
	   		$deplist=D("department")->where('id='.Auth::getdepartment())->select();
    	}
	    $this->assign('rolelist',$rolelist);
	    $this->assign('deplist',$deplist);
		$this->display();
    	}
    }
    public function infoimport_result(){//批量导入
    	$upload = new UploadFile(); // 实例化上传类
        $upload->maxSize=-1;
		if (!file_exists('./File/temp/'))
			mkdir('./File/temp/');
        $upload->savePath ='./File/temp/';
        $upload->allowExts = array('xls',);  
      	$upload->saveRule=time;
		if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $dir = $uploadList[0]['savepath'].$uploadList[0]['savename'];
            Vendor('Excel.reader');                         //加载第三方类
	    	$data = new Spreadsheet_Excel_Reader();
			$data->setOutputEncoding('utf-8');
			$data->read($dir); 
			for ($i =$_POST['hang'],$j=0; $i <= $data->sheets[0]['numRows']; $i++,$j++){
				$list[$i]['name']=$_POST['name']=trim($data->sheets[0]['cells'][$i][$_POST['lname']]);
				$list[$i]['cno']=$_POST['cno']=trim($data->sheets[0]['cells'][$i][$_POST['lcno']]);
				$list[$i]['no']=$_POST['no']=trim($data->sheets[0]['cells'][$i][$_POST['lno']]);
				$info=M('info');
				if($list[$i]['name']==''||$list[$i]['no']==''){
					$list[$i]['mes']='姓名或学号为空';
					$list[$i]['success']=0;
					continue;
				}
				if($info->create()){
					if($info->add()){
						$list[$i]['mes']='添加成功';
						$list[$i]['success']=1;
					}
					else{
						$list[$i]['mes']='已存在该用户不必重复导入';
						$list[$i]['success']=0;
					}
				}
				else{
					$list[$i]['mes']=$info->getError();
					$list[$i]['success']=0;
				}
			}
			$this->assign('list',$list);
            unlink($dir);
           	$c=M('info');
			$map['departmentid']=$_POST['departmentid'];
			$map['role']=array('neq','学生');
			$count=$c->where($map)->count();
			D('department')->where('id='.$_POST['departmentid'])->setfield('people',$count);
            $this->display();
        }   
    }
//课程     
	public function coursedetail(){//课程管理
		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生'){
			$this->redirect('auth/logon');
		}
		else {
			if (Auth::getrole()!='校级管理员')
    			$deplist=D("department")->where('id='.Auth::getdepartment())->select();
    		else
				$deplist=D("department")->select();
	    	$this->assign('deplist',$deplist);
	    	$name=$_GET['name'];
			$dname=$_GET['dname'];
			$Model = D("course_detail");
		if(!empty($_GET['name'])||($_GET['number'])||($_GET['dname'])){
			$map['name'] = array('like',$_GET['name'].'%');
			$map['number'] = array('like',$_GET['number'].'%');
			if ($_GET['dname'])
			$map['dname'] = array('eq',$_GET['dname']);
			if (Auth::getrole()!='校级管理员')
			$map['dname']=array('eq',Auth::getdepartmentname());
	    	$count = $Model->where($map)->count();    //计算总数 
	        $p = new Page ( $count, 20 ); 
	    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
    	}
		else {
			if (Auth::getrole()!='校级管理员')
			$map['dname']=array('eq',Auth::getdepartmentname());
			$count = $Model->count();    //计算总数 
	        $p = new Page ( $count, 20 ); 
			$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
		}
		foreach($list as $k=>$v){
			$Form = D('tcrelate');
			$map1['courseid']=array('eq',$v['id']);
			$map1['no']=array('eq',Auth::getno());
			$map2['courseid']=array('eq',$v['id']);
			$map2['no']=array('eq',Auth::getno());
			$map2['approve']=array('eq',1);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else 
				$list[$k]["isselect"]=false;
			if (Auth::getrole()=='校级管理员'||Auth::getrole()=='院系管理员'||$Form->where($map2)->select())
				$list[$k]["select"]=true;
			else
				$list[$k]["select"]=false;
		}
		$page = $p->show ();
		$this->assign ( "page", $page );
    	$this->assign('list',$list); // 模板发量赋值
    	$this->assign('role',Auth::getrole());// 模板发量赋值
		$this->display(); // 输出模板
	}
}
	public function coursedetail_jp(){//精品课程
		$form=D('course_detail');
		$list=$form->where('excellent=1')->select();
		$this->assign('list',$list);
		$this->display(); // 输出模板
	}
	public function coursedetail_all(){//所有课程
		$form=D('course_detail');
		$list=$form->select();
		$this->assign('list',$list);
		$this->display(); // 输出模板
	}
	public function courseadd_post(){//课程添加
		$form=D('department');
		$form->setInc('course','id='.$_POST['departmentid'],1);
		$course = D("course"); // 实例化模型类
		$_POST['pv']=0;
		$_POST['time']=date('Y-m-d H:i:s');
		if($course->Create()){// 创建数据对象
			if($course->add()){ // 写入数据库
				$this->ajaxReturn(0,'表单数据保存成功！',1);
			}
			else{
				$this->error('数据库写入错误',1);
			}
		}else{
			$this->error($course->getError(),1);
		}
	}
	public function courseadd(){//课程添加
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
    		if (Auth::getrole()!='院系管理员')
    			$deplist=D("department")->select();
    		else 
    		$deplist=D("department")->where('id='.Auth::getdepartment())->select();	
    	}
		//$deplist=D("department")->select();
    	$this->assign('deplist',$deplist);
		$this->display();
    } 
	public  function coursedelete(){//课程删除
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
    		$this->redirect('auth/logon');
    	else{
    	$_POST['departmentid']=D("course")->where('id='.$_POST['id'])->getfield('departmentid');
    	$form=D('department');
		$form->setDec('course','id='.$_POST['departmentid'],1);
		 $id=$_POST['id'];
		 $tcrelate=D('tcrelate');
		 $tcrelate->where('courseid='.$_POST['id'])->delete();
		 $course=D("course");
		 $course->delete($id);
		 $this->redirect('coursedetail');
    	}
    } 
	public  function courseedit(){//课程编辑
		$Form = D('tcrelate');
		$map['no']=array('eq',Auth::getno());
		$map['courseid']=array('eq',$_GET['id']);
		$map['approve']=array('eq',1);
		if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员'&&!($Form->where($map)->select()))
    		$this->redirect('auth/logon');
    	else{
    		if (Auth::getrole()!='校级管理员')
    			$deplist=D("department")->where('id='.Auth::getdepartment())->select();
    		else
				$deplist=D("department")->select();
		$clist=D("custom_detail")->where('courseid='.$_GET['id'])->select();
		$slist=array("","校级","上海市级","国家级");
		$this->assign('slist',$slist);
		$this->assign('clist',$clist);
    	$this->assign('deplist',$deplist);	
		if(!empty($_GET['id'])) { 
            $course=D("course_detail"); 
            $vo1=$course->getById($_GET['id']); 
            if($vo1) { 
            	$this->assign('id',$_GET['id']);
                $this->assign('vo1',$vo1); 
                $this->assign('number',$vo1['number']);
                $this->assign('coursename',$vo1['name']);
                $this->assign('departmentid',$vo1['departmentid']);
                $this->assign('nname',$vo1['nname']);
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
    } 
	}
	public function courseapprove()//课程认证_显示
	{
		if(Auth::getrole()=='校级管理员'){
			$count=D('tcrelate_detail')->count();
	    	$p=new Page($count, 20);
	    	$list=D('tcrelate_detail')->limit($p->firstRow.','.$p->listRows)->order('courseid')->select();
	    	$page=$p->show();
	    	$this->assign ( "page", $page );
			//$list=D('tcrelate_detail')->where('departmentid='.Auth::getdepartment())->select();123321
		}
		elseif (Auth::getrole()=='院系管理员'){
			$count=D('tcrelate_detail')->where('departmentid='.Auth::getdepartment())->count();
	    	$p=new Page($count, 20);
	    	$list=D('tcrelate_detail')->where('departmentid='.Auth::getdepartment())->limit($p->firstRow.','.$p->listRows)->order('courseid')->select();
	    	$page=$p->show();
	    	$this->assign ( "page", $page );
			//$list=D('tcrelate_detail')->where('departmentid='.Auth::getdepartment())->select();123321
		}
		else 
			$this->redirect('auth/logon');
		foreach($list as $k=>$v){
			$Form = D('tcrelate');
			$map1['id']=array('eq',$v['id']);
			$map1['approve']=array('eq',1);
			if($Form->where($map1)->select())
				$list[$k]["select"]=true;
			else
				$list[$k]["select"]=false;
		}
		foreach($list as $k=>$v){
			$Form = D('tcrelate');
			$map1['id']=array('eq',$v['id']);
			$map1['approve']=array('eq',2);
			if($Form->where($map1)->select())
				$list[$k]["select1"]=true;
			else
				$list[$k]["select1"]=false;
		}
		$this->assign('list',$list);
		//dump($list);
		$this->display();
	}
	public function approve()//课程认证_课程负责人
	{
		$tcrelate=D('tcrelate');
		$_POST['approve']=1;
		$tcrelate->create();
		$tcrelate->save();
		$this->redirect('courseapprove');
	}
	public function approve1()//课程认证_确定
	{
		$tcrelate=D('tcrelate');
		$_POST['approve']=2;
		$tcrelate->create();
		$tcrelate->save();
		$this->redirect('courseapprove');
	}
	public function approve2()//课程认证_取消
	{
		$tcrelate=D('tcrelate');
		$_POST['approve']=0;
		$tcrelate->create();
		$tcrelate->save();
		$this->redirect('courseapprove');
	}
    public function course(){//老师课程之间的关系
	    $map['courseid']=$_POST['id'];
	    $map['no']=Auth::getno();
	   	$data['courseid']=$_POST['id'];
	    $data['no']=Auth::getno();	    
	    if(Auth::getrole()!='校级管理员'&&Auth::getrole()!='院系管理员')
	    	$data['approve']=0;
	    else 
	    	$data['approve']=1;
	    $Form=D('tcrelate');
	    if($Form->where($map)->select())
			$Form->where($map)->delete();
		else 
			$Form->add($data);
		$this->redirect('coursedetail');
    }
	public function courseupdate() { //课程更新
		$course = D("course"); // 实例化模型类
		$departmentid=$course->where('id='.$_POST['id'])->getField('departmentid');
		if($_POST['number']!=$course->where('id='.$_POST['id'])->getField('number')&&$course->where('number='.$_POST['number'])->getField('id'))
			$this->error('课程序号已存在1!',1);
		elseif($_POST['departmentid']!=$departmentid){
				D('department')->setDec('course','id='.$departmentid,1);
				D('department')->setInc('course','id='.$_POST['departmentid'],1);
			}
			if($_POST['choose']==1)
				$_POST['url']=$_POST['url1'];
			elseif ($_POST['choose']==2)
				if (empty($_POST['url2']))
					$_POST['url']='about:blank';
				else 
					$_POST['url']='http://'.$_POST['url2'];
			if ($_POST['excellent']==''){
				$_POST['sort']=0;
				//$_POST['excellent']=0;
			}
			if ($_POST['sort']==''){
				$_POST['sort']=$course->where('id='.$_POST['id'])->getField('sort');
				//$_POST['excellent']=0;
			}		
			$course->create();
			if ($course->save())
			//dump($_POST);
				$this->ajaxReturn(0,'表单数据保存成功！',1);
			else{
				$this->error($course->getError(),1);
			}
    }
	public function coursesxupdate() { 
		$coursesx = D("coursesx"); // 实例化模型类
		if($_POST['url1'])
		$_POST['url']=$_POST['url1'];
		elseif ($_POST['url2'])
		$_POST['url']=$_POST['url2'];
		$coursesx->create();
		$coursesx->save();
		//dump($coursesx);
		//$_POST['time']=date('Y-m-d H:i:s'); 
		/*if($coursesx->create()){// 创建数据对象
			$coursesx->save();// 写入数据库
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($coursesx->getError(),1);
		}*/     
    }
    public function alterpw(){//修改密码界面
    	$this->display();
    }
    public function alterpw_post()//修改密码
    {
    	$info=D('info');
    	$pw=$info->where('no='.Auth::getno())->getfield('password');
    	if ($_POST['password1']!=$pw)
    		$this->error('原始密码不对！',1);
    	elseif ($_POST['password2']!=$_POST['password3'])	
    		$this->error('两次输入不一致！',1);
    	elseif (!$_POST['password2'])
    		$this->error('修改的密码不能为空！',1);
    	else
    		$info->where('no='.Auth::getno())->setfield('password',$_POST['password2']);
    	$this->success(); 		
    }
//教学班
public function classdetail()//教学班界面
    {
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	$form=D('class_detail');
    	$count=$form->where('no='.Auth::getno())->count();
    	$p=new Page($count, 20);
    	$list=$form->where('no='.Auth::getno())->limit($p->firstRow.','.$p->listRows)->select();
    	$page=$p->show();
    	$this->assign ( "page", $page );
    	$this->assign ( "name", Auth::getname());
		$this->assign('list',$list); // 模板发量赋值
		$this->display(); // 输出模板
    }
    public function classadd()//教学班添加界面
    {	
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	$form=D('tcrelate_detail');
    	$map['no']=Auth::getno();
    	$map['approve']=array('neq',0);
    	$list=$form->where($map)->select();
   		$this->assign('no',Auth::getno()); // 模板发量赋值
    	$this->assign('list',$list); // 模板发量赋值
		$this->display(); // 输出模板
    }
	public function class_add()//教学班添加
    {	
    	$_POST['id']=trim($_POST['id']);
    	$class = D("class"); // 实例化模型类
    	if (empty($_POST['id']))
    		$this->error('教学班序号不能为空',1);
    	elseif($class->find($_POST['id']))
    		$this->error('教学班已存在',1);
		elseif($class->Create()){// 创建数据对象
			$class->add(); // 写入数据库
			if (!file_exists('./File/class/'.$_POST['id'].'/')){
				$filename='./File/class/'.$_POST['id'].'/';
				mkdir(iconv("UTF-8", "GBK",$filename));
			}
			if (!file_exists('./File/class'.'/'.$_POST['id'].'/public/'))
	  		{
	  			$filename='./File/class'.'/'.$_POST['id'].'/public/';
	  			mkdir(iconv("UTF-8", "GBK",$filename));
	  		}
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($class->getError(),1);
		}
    } 
    public  function classdelete(){//教学班删除
		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		elseif ($_POST['id']){
			$class=D("class");
			$class->delete($_POST['id']);
			Dir::delDir('./File/class/'.$_POST['id']);
			//$this->redirect('classdetail');
		}
		$this->redirect('classdetail');
    } 
	public  function classedit(){//教学班编辑
		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
            $class=D("class_detail"); 
            $vo=$class->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
	public function classupdate() { //教学班更新
        $class=D("class"); 
        $_POST['id']=trim($_POST['id']);
        if (!$_POST['id'])
        	$this->error('教学班序号不能为空',1);
        elseif ($_POST['id']==$_POST['id1']){
        	$class->where('id='.$_POST['id1'])->setField('public',$_POST['public']);
        	$this->ajaxReturn(0,'表单数据保存成功！',1);
        }
        elseif ($class->find($_POST['id']))
    		$this->error('教学班已存在!',1);
    	else{
    		$class->where('id='.$_POST['id1'])->setField('id',$_POST['id']);
    		$class->where('id='.$_POST['id'])->setField('public',$_POST['public']);
			$dir='./File/class/';
    		rename($dir.$_POST['id1'],$dir.$_POST['id']);
			$this->ajaxReturn(0,'表单数据保存成功！',1);
    	}
    }
    public function classfile()//教学班文件夹
    {
		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
   			$id=$_GET['id'];
	   		$form=D('save');
	   		$map['classid']=array('eq',$_GET['id']);
			$count=$form->where($map)->count();
			$p=new Page($count, 20);
			$list=$form->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
   		$page = $p->show ();
   		$this->assign ( 'page', $page );
   		$this->assign('id',$_GET['id']);
   		$this->assign('list',$list);
   		$this->display(); // 输出模板
    } 

   public function savepath()//文件夹保存路径
   {
   		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
   		if (!file_exists('./File/class/'.$_GET['id'].'/'))
			mkdir('./File/class/'.$_GET['id'].'/');
    	$this->assign('id',$_GET['id']); // 模板发量赋值
		$this->display(); // 输出模板	
   }
 	public function save_add()//添加文件夹保存路径
   {	
   		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
   	 	$_POST['pname']=trim($_POST['pname']);
		//$_POST['pname']=$_POST['path'];
   		//$_POST['path']='/File/class'.'/'.$_POST['classid'].'/'.$_POST['path'].'/';
  		$save = D("save"); // 实例化模型类
  		if (!file_exists('./File/class'.'/'.$_POST['classid'].'/'.$_POST['pname'].'/'))
  		{
  			$filename='./File/class'.'/'.$_POST['classid'].'/'.$_POST['pname'].'/';
  			mkdir(iconv("UTF-8", "GBK",$filename));
  		}
		elseif ($_POST['pname']) 
			$this->error('文件夹已存在',1);
		if($save->Create()){// 创建数据对象
			$save->add(); // 写入数据库
			$this->ajaxReturn(0,'表单数据保存成功！',1);
			
		}else{
			$this->error($save->getError(),1);
		}
   }
	public function addstudent()//教学班添加学生
	{    	
	    //if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
	    		///$this->redirect('auth/logon');
	    //else{	
			$deplist=D("department")->select();
	    	$this->assign('deplist',$deplist);	
	    	//$no=$_GET['no'];
			//$name=$_GET['name'];
			//$dname=$_GET['dname'];
			$Model = D("info_detail");
			if(!empty($_GET['no'])||($_GET['name'])||($_GET['dname'])){
				$map['no'] = array('like','%'.$_GET['no'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['dname'])
				$map['dname'] = array('eq',$_GET['dname']);
				//if (Auth::getrole()!='校级管理员')
				//$map['dname']=array('eq',Auth::getdepartmentname());
				$map['role'] = array('eq','学生');
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('no')->select();
	    	}
			else {
				//if (Auth::getrole()!='校级管理员')
				//$map['dname']=array('eq',Auth::getdepartmentname());
				$map['role'] = array('eq','学生');
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
				$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('no')->select();
			}
	   		 foreach($list as $k=>$v){
				$Form = D('screlate');
				$map1['classid']=array('eq',$_GET['id']);
				$map1['no']=array('eq',$v['no']);
				if($Form->where($map1)->select())
					$list[$k]["isselect"]=true;
				else
					$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('id',$_GET['id']); 
			$this->assign('role',Auth::getrole());//传值到前台，做权限判断
			$this->display(); // 输出模板	
	    }
    //}
	public function student_add()//教学班添加学生
    {	
    	//dump($_POST['no']);
    	foreach($_POST['no'] as $k=>$v){    	
    	$screlate = M("screlate"); // 实例化模型类
    	$data['no']=$v;
    	$data['tno']=Auth::getno();
    	$data['classid']=$_POST['classid'];
    	$courseid=D('class')->where('id='.$_POST['classid'])->getfield('courseid');
    	$data['courseid']=$courseid;
    	$data['status']='未参加测试';
    	$aa=D("ccs_detail")->where('no='.$v.' '.'and'.' '.'courseid='.$courseid)->find();
    	//$aa=D("screlate")->where('no='.$v.' '.'and'.' '.'classid='.$_POST['classid'])->find();
    	if (empty($aa))
			$screlate->add($data); // 写入数据库	
		else 
			exit('该学生已被其他老师选择！');	
		}
		//dump($_POST['classid']);
		$this->redirect('addstudent',array('id'=>$_POST[classid]));
    } 
    public function deletestudent()//教学班删除学生
    {
     if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
		$Model = D("screlate_detail");
		if(!empty($_GET['no'])||($_GET['name'])){
			$map['no'] = array('like','%'.$_GET['no'].'%');
			$map['name'] = array('like','%'.$_GET['name'].'%');
			$map['classid'] = array('eq',$_GET['id']);
			$count = $Model->where($map)->count();    //计算总数 
	        $p = new Page ( $count, 50 ); 
	    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('no')->select();
    	}
		else {
			$map['classid'] = array('eq',$_GET['id']);
			$count = $Model->where($map)->count();    //计算总数 
	        $p = new Page ( $count, 50 ); 
			$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('no')->select();
		}
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('list',$list); // 模板发量赋值
		$this->assign('id',$_GET['id']); 
		$this->assign('role',Auth::getrole());//传值到前台，做权限判断
		$this->display(); // 输出模板
    }
    public function student_delete()//教学班删除学生
    {	
    	foreach($_POST['id'] as $k=>$v){    	
    	$screlate = D("screlate"); // 实例化模型类
    	//$screlate->Create();
    	//$screlate->add();
    	//dump($screlate);
		$screlate->delete($v); 	
		}
		$this->redirect('deletestudent',array('id'=>$_POST[classid]));
    } 
	public function studentupload() {//学生上传
		if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (!empty($_GET['id'])){
		$list=D('save')->where('classid='.$_GET['id'])->select();
		$this->assign('classid',$_GET['id']);
		$this->assign('list',$list);
		$this->display();
		 if (!empty($_FILES)) { 
            //如果有文件上传 上传附件 
            $this->_studentupload(); 
            //$this->forward(); 
		}
		}
		else 
			exit('课程不存在');       
    } 
	public function _studentupload() { //学生上传   
		$save=D('save');
		$pname=$save->where('id='.$_POST['id'])->getField('pname');
		$filename=$_FILES['image']['name']; //单独输出filename是可以得出值的
		$filetype=end(explode(".",$filename)); 
		$_FILES['image']['name']=Auth::getno().'-'.Auth::getname().'.'.$filetype;
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize=-1;
        //$upload->savePath =iconv("UTF-8", "GBK", './File/class/'.$_POST['id'].'/'.$_POST['pname'].'/');
        //echo iconv("UTF-8", "GBK", './File/class/'.$_POST['id'].'/'.$_POST['pname'].'/');
        $upload->savePath ='./File/class/'.$_POST['classid'].'/'.$pname.'/';
      	$upload->allowExts=array(); 
      	//$upload->saveRule=time;
		if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $_POST['name'] = $uploadList[0]['name'];
            //$_POST['savename'] = $uploadList[0]['savename'];
            $_POST['extension'] = $uploadList[0]['extension'];
            $_POST['path'] = $uploadList[0]['savepath'];
            $_POST['size'] = $uploadList[0]['size'];
        $model = M('work');
        $map['name']=$_POST['name'];
        $map['classid']=$_POST['classid'];
        $map['saveid']=$_POST['id'];
        if($model->where($map)->count())
        	$model->where($map)->delete();
        //保存当前数据对象
        $data['nname'] = Auth::getname();
        $data['no']=Auth::getno();
        $data['name'] = $_POST['name'];
        $data['savename'] = Auth::getno().'-'.Auth::getname().'.'.$_POST['extension'];
        $data['size']= $_POST['size'];
        $data['path']= substr ($_POST['path'], 1);
        $data['saveid']= $_POST['id'];
       	//$data['downloadno']= 0;
        $data['classid']= $_POST['classid'];
        $data['time'] = date('Y-m-d H:i:s');
        $list = $model->add($data);
        if ($list !== false) { 
            $this->success('上传成功！'); 
        } else { 
            $this->error('上传失败!'); 
        }
        } 
   }
 	public function handin()//作业提交
    {
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('screlate_detail');
    	$list=$form->where('no='.Auth::getno())->select();
    	$this->assign('list',$list);
    	$this->display();
    }
	public function handinall()//作业提交
    {
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('class_detail');
    	$list=$form->where('public')->select();
    	$this->assign('list',$list);
    	$this->display();
    }
    public function workdetail()//作业
    {	
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	$save=D('save');
    	$pname=$save->where('id='.$_GET['p'])->getField('pname');
    	$work=D('work');
    	$list=$work->where('saveid='.$_GET['p'])->select();
   	 	foreach($list as $k=>$v){
			$work = D('work');
			if($work->where('id='.$v['id'])->getField('text'))
				$list[$k]["a"]=true;
			else
				$list[$k]["a"]=false;
		}
    	$this->assign('classid',$_GET['id']);
    	$this->assign('p',$_GET['p']);
    	$this->assign('pname',$pname);
    	$this->assign('list',$list);
    	$this->display();
    }
 	public function workdetail1()//作业查重
    {	
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	$save=D('save');
    	$pname=$save->where('id='.$_GET['p'])->getField('pname');
    	$work=D('work');
    	$list=$work->where('saveid='.$_GET['p'])->select();
    	$this->assign('classid',$_GET['id']);
    	$this->assign('p',$_GET['p']);
    	$this->assign('pname',$pname);
    	$this->assign('list',$list);
    	$this->display();
    }
 	public function check(){//作业查重添加
   		$form=D('check');
   		$data['classid']=$_POST['classid'];
   		$data['saveid']=$_POST['saveid'];
   		$data['time']=date('Y-m-d H:i:s');
   		$data['no']=Auth::getno();
   		$form->create();
   		$form->add($data);
   		$id=D('check')->where($data)->getfield('id'); //查重id
		//pclose(popen("python ./compare/compare.py ".$id, 'r'));    	
		foreach($_POST['url'] as $k=>$v){    	
    	$form = M("checkrelate"); // 实例化模型类
    	//$v=str_ireplace("/","\\",$v,"5");
    	$data1['url']=$v;
    	//$data1['url']=str_replace("/","\\",$data1['url'],"5");
    	$data1['checkid']=D('check')->where($data)->getfield('id');
    	$data1['no']=D('check')->where($data)->getfield('no');
		$form->add($data1); // 写入数据库		
		}
		//$list=D('checkrelate')->where('checkid='.$data1['checkid'])->select();
		//$this->assign('list',$list);
		//dump($list);
		//$this->display();
		$this->redirect('check1');
    }
    public function check1(){//作业查重显示
    	$list=D('check')->where('no='.Auth::getno())->select();
    	$this->assign('list',$list);
    	$this->display();
    }
    public function check_detail(){//作业查重显示
    	$list=D('checkrelate')->where('checkid='.$_GET['id'])->select();
    	foreach($list as $k=>$v){
			$Form = D('tcrelate');
			$list[$k]["url"]=substr (strrchr($v['url'],'/'),1);
    	}
		$this->assign('list',$list);
		//dump($list);
		$this->display();
    }
 	public function work_detail()//作业管理_学生
    {	
		if (Auth::getrole()!='学生')
			$this->redirect('auth/logon');
    	$save=D('save');
    	$pname=$save->where('id='.$_GET['p'])->getField('pname');
    	$work=D('work_detail');
    	$list=$work->where('no='.Auth::getno())->select();
    	$this->assign('classid',$_GET['id']);
    	$this->assign('p',$_GET['p']);
    	$this->assign('pname',$pname);
    	$this->assign('list',$list);
    	$this->display();
    }
     public function pathedit()//上传路径编辑
   {
   	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
   	if(!empty($_GET['id'])) { 
            $save=D("save"); 
            $vo=$save->getByid($_GET['id']); 
            if($vo) { 
            	$this->assign('id',$vo['classid']);
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
   }
    public function pathupdate() { //上传路径更新
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	$_POST['pname']=trim($_POST['pname']);
    	$save=D("save");
    	if($save->create()){
    		if (file_exists('./File/class'.'/'.$_POST['classid'].'/'.$_POST['pname'].'/'))
    		$this->error('文件夹已存在',1);
    		$save->save(); 
    		$dir='./File/class/'.$_POST['classid'].'/';
    		//mkdir(iconv("UTF-8", "GBK",$filename));
    		//iconv("UTF-8", "GBK", $filename);
    		rename(iconv("UTF-8", "GBK",$dir.$_POST['path']),iconv("UTF-8", "GBK",$dir.$_POST['pname']));
			$this->success('保存成功',1);
    	}
       	else{
       		$this->error($save->getError(),1);
       	}      
    } 
    public  function pathdelete(){//上传路径删除
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		if (Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		$id=$_POST['id'];
		$save=D("save");
		//$a=$save->where('id='.$_POST['id'])->getfield(savename);
		if ($_POST['pname'])
		Dir::delDir(iconv("UTF-8", "GBK",'./File/class/'.$_POST['classid'].'/'.$_POST['pname']),$subdir=true);
		//else 
		$save->delete($id);
		$this->redirect('classfile',array('id'=>$_POST['classid']));
    } 
    public  function workdelete(){//作业删除
    	if (Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		$work=D("work");
		$name=$work->where('id='.$_POST['id'])->getField('name');
		$filename=iconv("UTF-8", "GBK",'./File/class/'.$_POST['classid'].'/'.$_POST['pname'].'/'.$name);
		@unlink($filename);
		//@unlink('./File/class/'.$_POST['classid'].'/'.$_POST['pname'].'/'.$name);
		$work->delete($_POST['id']);
		$this->redirect('workdetail',array('id'=>$_POST['classid'],'p'=>$_POST['p']));
    } 
    public function filedownload()//文件下载
    {
    	Vendor('php_zip.zip');                         //加载第三方类
		$zip = new PHPZip(); 
		$filename=iconv("UTF-8", "GBK",'./File/class/'.$_POST['classid'].'/'.$_POST['pname']);
		$zip -> createZip($filename, $_POST[pname].'.zip',1);
		//$zip -> downloadZip("./File/class/123/1", "1.zip");//自动下载
    }
 	public function addcustom()//课程自定义页面
    {
    	$this->assign('courseid',$_GET['id']);
    	$this->display();
    }
	public function custom_add()//课程自定义页面添加
	{	
		$custom = D('custom'); // 实例化模型类
		$_POST['time']=date('Y-m-d H:i:s'); 
		$_POST['pv']=0;
		$_POST['no']=Auth::getno();
		//$custom->create();
		//$custom->add();
		//$this->redirect('course/course',array('id'=>$_POST['courseid'],'id'=>$_POST['id']));
		if($custom->create()){
			if($custom->add())
				$this->success('表单提交成功');
			else
				$this->error('数据库插入错误');
		}
		$this->error($custom->getError());
	}
	public function customedit()//课程自定义页面编辑
	{	
		if(!empty($_POST['id'])) { 
            $vo=D("custom_detail")->getById($_POST['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
	}
public function customdelete()//课程自定义页面删除
	{	
		if(!empty($_POST['id'])) { 
            D("custom")->delete($_POST['id']); 
               $this->redirect('courseedit',array('id'=>$_POST['courseid']));
        }else{ 
            exit('删除项不存在！'); 
        } 
	}
	public function customupdate() { //课程自定义页面更新
		$custom = D("custom"); // 实例化模型类
		$_POST['time']=date('Y-m-d H:i:s'); 
		$_POST['pv']=0;
		$_POST['no']=Auth::getno();
		if($custom->create()){// 创建数据对象
			$custom->save();// 写入数据库
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($custom->getError(),1);
		}          
    }

	public function adddcustom()//院系自定义页面
    {
    	if (D('custom1')->where('departmentid='.$_GET['id'])->count())
    		//$this->error('院系主页已存在！',1);
    		 exit('院系主页已存在！'); 
    	else {
    		$this->assign('departmentid',$_GET['id']);
    		$this->display();
    	}
    }
	public function dcustom_add()//院系自定义页面添加
	{	
		$custom = D('custom1'); // 实例化模型类
		$_POST['time']=date('Y-m-d H:i:s'); 
		$_POST['pv']=0;
		$_POST['no']=Auth::getno();
		//$custom->create();
		//$custom->add();
		//$this->redirect('depart/depart',array('id'=>$_POST['departid'],'id'=>$_POST['id']));
		if($custom->create()){
			if($custom->add())
				$this->success('表单提交成功');
			else
				$this->error('数据库插入错误');
			}else{
				$this->error($custom->getError(),1);
		}          
	}
	public function dcustomedit()//院系自定义页面编辑
	{	
		if(!empty($_POST['id'])) { 
            $vo=D("custom1_detail")->getById($_POST['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('未建立院系主页！'); 
            } 
        }else{ 
            exit('未建立院系主页！'); 
        } 
	}
	public function dcustomdelete()//院系自定义页面删除
	{	
		if(!empty($_POST['id'])) { 
            D("custom1")->delete($_POST['id']); 
               $this->redirect('collegeedit',array('id'=>$_POST['departmentid']));
        }else{ 
            exit('未建立院系主页！'); 
        } 
	}
	public function dcustomupdate() { //院系自定义页面更新
		$custom = D("custom1"); // 实例化模型类
		$_POST['time']=date('Y-m-d H:i:s'); 
		$_POST['pv']=0;
		$_POST['no']=Auth::getno();
		if($custom->create()){// 创建数据对象
			$custom->save();// 写入数据库
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($custom->getError(),1);
		}          
    }
    public function remark()//作业点评
    {
    	$vo=D('work')->getById($_GET['id']); 
    	$this->assign('vo',$vo);
    	$this->display();
    }
	public function workremark() { //作业点评
		$work = D("work"); // 实例化模型类
		if($work->create()){// 创建数据对象
			$work->save();// 写入数据库
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($work->getError(),1);
		}          
    }
//测试
	public  function  testdetail(){//测试管理
		if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	else{
			$Model = D("test_detail");
			$map['no']=array('eq',Auth::getno());
			$count = $Model->where($map)->count();    //计算总数 
	    	$p = new Page ( $count, 20 ); 
			$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();			
		}
		$page = $p->show ();
		$this->assign ( "page", $page );
		$this->assign('list',$list); // 模板发量赋值
		$this->assign('role',Auth::getrole());//传值到前台，做权限判断
		$this->display(); // 输出模板
    }
    public function testadd(){//测试添加_显示
    	if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	else{
    		$map['no']=Auth::getno();
    		$map['approve']=array('neq',0);
	   		$clist=D("tcrelate_detail")->where($map)->select();
	   		$typelist=array('练习','测试');
	    	$this->assign('no',Auth::getno());
	   		$this->assign('clist',$clist);
	   		$this->assign('typelist',$typelist);
			$this->display();
    	}
    } 
    public function testadd_post(){//测试添加_更新
    	if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	$test = D("test"); // 实例化模型类
    	$_POST['time']=date('Y-m-d H:i:s');
    	if ($_POST['type']=='测试')
    		$_POST['type1']=0;
    	elseif ($_POST['type']=='练习')
    		$_POST['type1']=1;
    	$data=$test->Create();
		if($data){// 创建数据对象
			$test->add(); // 写入数据库
			$this->ajaxReturn($data,'表单数据保存成功！',1);
		}else{
			$this->error($test->getError(),1);
		}
    } 
   public  function testdelete(){//测试删除
   	if(Auth::getrole()!='学生'&&Auth::getrole()!='访客')
   	{
		$test=D("test");
		$test->delete($_POST['id']);
		$this->redirect('testdetail');
   	}
		else 
			$this->redirect('auth/logon');
    } 
    public  function starttest(){//开始测试
   	if(Auth::getrole()!='学生'&&Auth::getrole()!='访客')
   	{
		$test=D("test");
		$type=$test->where('id='.$_POST['id'])->getfield('type');
		$list=D('test_detail')->where('id='.$_POST['id'])->select();
		if ($type=='测试'){
			$courseid=$test->where('id='.$_POST['id'])->getfield('courseid');
			$map['tno']=Auth::getno();
			$map['courseid']=$courseid;
			$list1=D('screlate_detail')->where($map)->select();
			$test->where('id='.$_POST['id'])->setfield('type1','1');
			$this->assign('tno',Auth::getno());
			$this->assign('courseid',$courseid);
			$this->assign('list',$list);
			$this->assign('list1',$list1);
			$this->display();
		}
		else 
			exit('该卷不是测试卷');
   	}
		else 
			$this->redirect('auth/logon');
    } 
     public  function finishtest(){//开始测试
   	if(Auth::getrole()!='学生'&&Auth::getrole()!='访客')
   	{
		$test=D("test");
		$type=$test->where('id='.$_POST['id'])->getfield('type');
		$list=D('test_detail')->where('id='.$_POST['id'])->select();
		if ($type=='测试'){
			$map['courseid']=$_POST['courseid'];
    		$map['tno']=$_POST['tno'];
    		D('screlate')->where($map)->setfield('status','未参加测试');
			$test->where('id='.$_POST['id'])->setfield('type1','0');
			$this->assign('list',$list);
			$this->redirect('choose1');
		}
		else 
			exit('该卷不是测试卷');
   	}
		else 
			$this->redirect('auth/logon');
    } 
    public  function testpackage(){//测试删除
   	if(Auth::getrole()!='学生'&&Auth::getrole()!='访客')
   	{
		$this->assign('id',$_GET['id']);
		$this->display();
   	}
		else 
			$this->redirect('auth/logon');
    } 
    public function testcorrect()//改卷
    {
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('tcrelate_detail');
    	$list=$form->where('no='.Auth::getno())->select();
    	$this->assign('list',$list);
    	$this->display();
    }
	public function test_correct()//改卷
    {
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('test_detail');
    	$list=$form->where('courseid='.$_GET['id'])->select();
    	$this->assign('courseid',$_GET['id']);
    	$this->assign('list',$list);
    	$this->display();
    }
	public function correct_test()//改卷
    {
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('ccs_detail');
    	$list=$form->where('courseid='.$_GET['courseid'])->order('no')->select();
    	$this->assign('courseid',$_GET['courseid']);
    	$this->assign('testid',$_GET['id']);
    	$this->assign('list',$list);
    	$this->display();
    }
public function correcttest1()//单选改卷
    {
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('choice_contrast');
    	$map['testid']=$_GET['testid'];
    	$map['no']=$_GET['no'];
    	//$map['number']=$_GET['number'];
    	$list=$form->where($map)->order('id')->select();
    	foreach ($list as $k=>$v){
    	if($v['answer']==$v['answer1'])
				$list[$k]["i"]=true;
    	}
    	$this->assign('testid',$_GET['testid']);
    	$this->assign('courseid',$_GET['courseid']);
    	$this->assign('no',$_GET['no']);
    	$this->assign('list',$list);
    	$this->display();
    }
	public function correcttest2()//多选改卷
    {
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('nchoice_contrast');
    	$map['testid']=$_GET['testid'];
    	$map['no']=$_GET['no'];
    	//$map['number']=$_GET['number'];
    	$list=$form->where($map)->order('id')->select();
    	$list=$form->where($map)->order('id')->select();
    	foreach ($list as $k=>$v){
    	if($v['answer']==$v['answer1'])
				$list[$k]["i"]=true;
    	}
    	$this->assign('testid',$_GET['testid']);
    	$this->assign('courseid',$_GET['courseid']);
    	$this->assign('no',$_GET['no']);
    	$this->assign('list',$list);
    	$this->display();
    }
	public function correcttest3()//填空改卷
    {
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('blanksave_detail');
    	$map['testid']=$_GET['testid'];
    	$map['no']=$_GET['no'];
    	$map['number']=$_GET['number'];
    	$number1=$_GET['number']+1;
    	$list=$form->where($map)->order('id')->select();
    	$this->assign('testid',$_GET['testid']);
    	$this->assign('courseid',$_GET['courseid']);
    	$this->assign('no',$_GET['no']);
    	$this->assign('number1',$number1);
    	$this->assign('list',$list);
    	if(empty($list))
    	{
    		$courseid=D(test_detail)->where('id='.$_GET['testid'])->getfield('courseid');
    		//$this->display();
    		$this->redirect('correcttest4',array('courseid'=>$_GET['courseid'],'testid'=>$_GET['testid'],'no'=>$_GET['no'],'number'=>1));
    	}	
    	else
    		$this->display();
    }
	public function correcttest4()//简答改卷
    {
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	$form=D('sasave_detail');
    	$map['testid']=$_GET['testid'];
    	$map['no']=$_GET['no'];
    	$map['number']=$_GET['number'];
    	$number1=$_GET['number']+1;
    	$list=$form->where($map)->order('id')->select();
    	$this->assign('testid',$_GET['testid']);
    	$this->assign('courseid',$_GET['courseid']);
    	$this->assign('no',$_GET['no']);
    	$this->assign('number1',$number1);
    	$this->assign('list',$list);
    	if(empty($list))
    	{
    		$courseid=D(test_detail)->where('id='.$_GET['testid'])->getfield('courseid');
    		$this->redirect('correct_test',array('courseid'=>$courseid,'id'=>$_GET['testid']));
    	}	
    	else
    		$this->display();
    }
    public function choice_score()//学生得分
    {	
		if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		else {
			foreach ($_POST['score'] as $k=>$v){
				$score=$score+$v;
			}			
			$aa=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->find();
			$score1=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('score1');
			if (empty($aa)){
		    	$form=D('score');
		    	$data['score']=$score;
		    	$data['no']=$_POST['no'];
		    	$data['testid']=$_POST['testid'];
		    	$form->create();
		    	$form->add($data);
		    	D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->setfield('score1',$score);
			}
			elseif ($score!=$score1){
				D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->setfield('score',$score);
				//$this->redirect('correcttest2',array('courseid'=>$_POST['courseid'],'testid'=>$_POST['testid'],'no'=>$_POST['no']));
			}
			//dump($_POST['score']);
			$this->redirect('correcttest2',array('courseid'=>$_POST['courseid'],'testid'=>$_POST['testid'],'no'=>$_POST['no']));
		}
    }	
    public function nchoice_score()//学生得分
    {	
		if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		else {
			foreach ($_POST['score'] as $k=>$v){
				$score=$score+$v;
			}
			$bb=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('score2');			
			$aa=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->find();
			if (empty($aa)){
		    	$form=D('score');
		    	$data['score']=$score;
		    	$data['no']=$_POST['no'];
		    	$data['testid']=$_POST['testid'];
		    	D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->setfield('score2',$score);
		    	$form->create();
		    	$form->add($data);		    	
			}
			elseif (empty($bb)){
				D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->setfield('score2',$score);
				$score1=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('score');
				$score=$score+$score1;
				$id=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('id');	
				//$score=$_POST['score']+$score1-$score2;
				D('score')->where('id='.$id)->setfield('score',$score);	
			}
			$this->redirect('correcttest3',array('courseid'=>$_POST['courseid'],'testid'=>$_POST['testid'],'no'=>$_POST['no'],'number'=>1));
		}
    }	
    public function mytest()//学生测试
    {	
		if (Auth::getrole()!='学生')
			$this->redirect('auth/logon');
    	//$save=D('save');
    	//$pname=$save->where('id='.$_GET['p'])->getField('pname');
    	$form=D('mytest_detail');
    	$list=$form->where('no='.Auth::getno())->select();
    	//$this->assign('classid',$_GET['id']);
    	//$this->assign('p',$_GET['p']);
    	//$this->assign('pname',$pname);
    	$this->assign('list',$list);
    	$this->display();
    }	
	public function detail(){//预览
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$Model=D('saqt_detail');
    		$list4=$Model->where('testid='.$_GET['id'])->order('number')->select();
    		$Model=D('choicet_detail');
    		$list1=$Model->where('testid='.$_GET['id'])->order('number')->select();
    		$Model=D('nchoicet_detail');
    		$list2=$Model->where('testid='.$_GET['id'])->order('number')->select();
    		$Model=D('blankt_detail');
    		$list3=$Model->where('testid='.$_GET['id'])->order('number')->select();
			$this->assign('list1',$list1); // 模板发量赋值
			$this->assign('list2',$list2); // 模板发量赋值
			$this->assign('list3',$list3); // 模板发量赋值
			$this->assign('list4',$list4); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			//$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
    	}
	}
	public function choice_case()//选择情况分析
	{
		$a=D('choicerelate')->where('testid='.$_GET['id'])->count();
		for ($i=1;$i<=$a;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=A;
			$count=D('chsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=A;		
			$data['count']=$count;	
			$data['type']=1;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=A;
			$map1['type']=1;
			$aa=D('chcase')->where($map1)->find();
			if (empty($aa))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
			}
		}
		for ($i=1;$i<=$a;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=B;
			$count=D('chsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=B;		
			$data['count']=$count;	
			$data['type']=1;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=B;
			$map1['type']=1;
			$aa=D('chcase')->where($map1)->find();
			if (empty($aa))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
			}
		}
		for ($i=1;$i<=$a;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=C;
			$count=D('chsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=C;		
			$data['count']=$count;	
			$data['type']=1;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=C;
			$map1['type']=1;
			$aa=D('chcase')->where($map1)->find();
			if (empty($aa))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		for ($i=1;$i<=$a;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=D;
			$count=D('chsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=D;		
			$data['count']=$count;	
			$data['type']=1;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=D;
			$map1['type']=1;
			$aa=D('chcase')->where($map1)->find();
			if (empty($aa))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		$b=D('nchoicerelate')->where('testid='.$_GET['id'])->count();
		for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=A;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=A;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=A;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=AB;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=AB;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=AB;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=ABC;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=ABC;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=ABC;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=ABCD;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=ABCD;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=ABCD;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=B;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=B;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=B;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=BC;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=BC;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=BC;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=BCD;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=BCD;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=BCD;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
	for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=C;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=C;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=C;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
	for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=CD;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=CD;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=CD;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
	for ($i=1;$i<=$b;$i=$i+1)
		{
			$map['testid']=$_GET['id'];
			$map['number']=$i;
			$map['answer']=D;
			$count=D('nchsave')->where($map)->count();
			//$count=D('nchsave')->where($map)->count();
			$data['testid']=$_GET['id'];
			$data['number']=$i;
			$data['answer']=D;		
			$data['count']=$count;	
			$data['type']=2;
			$map1['testid']=$_GET['id'];
			$map1['number']=$i;	
			$map1['answer']=D;
			$map1['type']=2;
			$bb=D('chcase')->where($map1)->find();
			if (empty($bb))
			{
				$form = D("chcase"); // 实例化模型类
				$form->Create();// 创建数据对象
				$form->add($data); // 写入数据库
				//dump($count);
			}
		}
		$this->redirect('chcase',array('id'=>$_GET['id']));
	}
	public function chcase()//选择情况分析显示
	{
		$courseid=D('test')->where('id='.$_GET['id'])->getfield('courseid');
		$form=D('chcase');
		$map['testid']=$_GET['id'];
		$map['type']=1;
		$list1=$form->where($map)->order('number')->select();
		$map1['testid']=$_GET['id'];
		$map1['type']=2;
		$list2=$form->where($map1)->order('number')->select();
		$this->assign('courseid',$courseid);
		$this->assign('list1',$list1);
		$this->assign('list2',$list2);
		$this->display();		
	}
//简答题
 	public function sa_save()//评分
     {	  
     	$score3=D('sasave')->where('id='.$_POST['id'])->getfield('score1');	
    	if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		elseif ($_POST['score']<=$score3&&$_POST['score']>0) {
			//$form=D('blanksave');
			$score2=D('sasave')->where('id='.$_POST['id'])->getfield('score');
			D('sasave')->where('id='.$_POST['id'])->setfield('score',$_POST['score']);
			$aa=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->find();
			if (empty($aa)){
		    	$form=D('score');
		    	$data['score']=$_POST['score'];
		    	$data['no']=$_POST['no'];
		    	$data['testid']=$_POST['testid'];
		    	$form->create();
		    	$form->add($data);
		    	$this->ajaxReturn(0,'表单数据保存成功！',1);
			}
			else {
				$score1=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('score');
				$id=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('id');	
				$score=$_POST['score']+$score1-$score2;
				D('score')->where('id='.$id)->setfield('score',$score);	
				$this->ajaxReturn(0,'表单数据保存成功！',1);
			}
			//$_POST['number']=$_POST['number']+1;
			//dump($score2);dump($score1);dump($_POST['score']);dump($_POST['id']);
			//$this->redirect('correcttest4',array('courseid'=>$_POST['courseid'],'testid'=>$_POST['testid'],'no'=>$_POST['no'],'number'=>$_POST['number']));
		}
		else 
			$this->error('输入分值有误!',1);
    } 
	public function mytest_saq(){//简答题_学生
		if(Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	else{
    		$type1=D('test')->where('id='.$_GET['id'])->getfield('type1');
    		if ($type1=='0')
    			exit('测试还未开始!');
    		else {
    			$courseid=D('test')->where('id='.$_GET['id'])->getfield('courseid');
    			$map['courseid']=$courseid;
    			$map['no']=Auth::getno();
    			D('screlate')->where($map)->setfield('status','正在测试');
	    		$Model=D('saqt_detail');
				//$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
			   //$p = new Page ( $count, 20 ); 
				$list=$Model->where('testid='.$_GET['id'].' '.'and'.' '.'number='.$_GET['number'])->select();
				if (!empty($list)){
					//$page = $p->show ();
					//$this->assign ( "page", $page );
					$this->assign('number1',$_GET['number']); // 模板发量赋值
					$number=$_GET['number']+1;
					$number2=$_GET['number']-1;
					$this->assign('list',$list); // 模板发量赋值
					$this->assign('testid',$_GET['id']); // 模板发量赋值
					$this->assign('number',$number); // 模板发量赋值
					$this->assign('number2',$number2); // 模板发量赋值
					$this->assign('no',Auth::getno()); // 模板发量赋值
					//dump($list);
					$this->display(); // 输出模板
				}
				else 
					$this->redirect('mytestend',array('courseid'=>$courseid,'testid'=>$_GET['id'],'no'=>Auth::getno()));
	    	}
    	}
	}
	public function saqtdetail1(){//简答题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			exit('权限不够!');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('saq_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
		    	$map['departmentid'] = array('eq',Auth::getdepartment());
				$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('saqrelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['saqid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function saqadd(){//简答添加
		if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    	}
    	$this->assign('cnlist',$cnlist);
    	$this->assign('no',Auth::getno()); // 模板发量赋值
		$this->display();
    } 
	public function saqdetail(){//简答题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('saq_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
				$list=$Model->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('saqrelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['saqid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function saq_add(){//简答题添加
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$saq = D("saq"); // 实例化模型类
			if($saq->Create()){// 创建数据对象
				if($saq->add()){ // 写入数据库
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}
				else{
					$this->error('数据库写入错误',1);
				}
			}else{
				$this->error($saq->getError(),1);
			}

		}
    } 
	public  function saqdelete(){//简答题删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$saq=D("saq");
			$saq->delete($id);
			$this->redirect('saqdetail');
		}
    } 
	public  function saqedit(){//简答题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
			$cname=D('saq_detail')->where('id='.$_GET['id'])->getfield('cname');
			$this->assign('cnlist',$cnlist);
			$this->assign('cname',$cname);
            $saq=D("saq"); 
            $vo=$saq->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
	public function saqupdate() { //简答题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $saq=D("saq"); 
		if($saq->Create()){// 创建数据对象
			$saq->save();
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($saq->getError(),1);
		}
    } 
	public function saqtdetail2(){//简答题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$Model=D('saqt_detail');
			$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
		    $p = new Page ( $count, 20 ); 
			$list=$Model->where('testid='.$_GET['id'])->limit($p->firstRow.','.$p->listRows)->order('number')->select();
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			//$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
    	}
	}
    public function saqt_add()//简答组卷
    {	
    	if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
	    	//dump($_POST['no']);
	    	foreach($_POST['id'] as $k=>$v){    	
		    	$saqrelate = M("saqrelate"); // 实例化模型类
		    	$data['saqid']=$v;
		    	$data['testid']=$_POST['testid'];
		    	//$saqrelate->Create();
		    	//$saqrelate->add();
		    	//dump($saqrelate);
		    	$aa=D("saqrelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'saqid='.$v)->find();
		    	//$aa=D("saqrelate")->where('testid=2 and saqid=9')->find();
		    	//dump($aa);
		    	//dump($_POST['id']);
		    	//echo $_POST['testid'];
		    	//echo $v;
		    	if (empty($aa))
		    		$saqrelate->add($data); // 写入数据库	
		    	/*else {
					$saqrelate->add($data); // 写入数据库		
					$this->redirect('saqtdetail2',array('id'=>$_POST['testid']));
					}*/
	    	}
    	}
		$this->redirect('saqtdetail2',array('id'=>$_POST['testid']));
    } 
    public  function saqtedit(){//简答题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			//$cnlist=D('course')->select();
			//$cname=D('saq_detail')->where('id='.$_GET['id'])->getfield('cname');
			//$this->assign('cnlist',$cnlist);
			//$this->assign('cname',$cname);
            $saq=D("saqrelate"); 
            $vo=$saq->getById($_GET['id']); 
            if($vo) { 
            	$this->assign('testid',$_GET['testid']);
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
    public function saqtupdate() { //简答题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $saqrelate=D("saqrelate"); 
        if ($_POST['number']!=$_POST['number1'])
	        $aa=D("saqrelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'number='.$_POST['number'])->find();
	        if ($aa)
	        	$this->error('题号已存在',1);
	        else{
		        if($saqrelate->Create()){// 创建数据对象
					$saqrelate->save();
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}else{
					$this->error($saqrelate->getError(),1);
				}
	        }
    } 
    public  function saqtdelete(){//简答题组卷删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$saq=D("saqrelate");
			$saq->delete($id);
			$this->redirect('saqtdetail2',array('id'=>$_POST['testid']));
		}
    } 
    public function sasave()//简答题答案保存
    {	  	
    	$sasave = M("sasave"); // 实例化模型类
    	$map['testid']=array('eq',$_POST['testid']);
    	$map['saqid']=array('eq',$_POST['saqid']);
    	$map['no']=array('eq',$_POST['no']);
    	$aa=D("sasave")->where($map)->find();
    	if (empty($aa)){
	    	$data['saqid']=$_POST['saqid'];
	    	$data['testid']=$_POST['testid'];
	    	$data['no']=$_POST['no'];
	    	$data['answer']=$_POST['answer'];
	    	$data['number']=$_POST['number1'];
	    	$data['score1']=$_POST['score1'];
	    	$sasave->Create();
	    	$sasave->add($data);
    	}
    	else 	
    		$sasave->where($map)->setfield('answer',$_POST['answer']);
	    	//$sasave->save();
		$this->redirect('mytest_saq',array('id'=>$_POST['testid'],'number'=>$_POST['number']));
    } 
    //单选题
	public function mytest_choice(){//单选题_学生
		if(Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	else{
    		$type1=D('test')->where('id='.$_GET['id'])->getfield('type1');
    		if ($type1=='0')
    			exit('测试还未开始!');
    		else {
    			$courseid=D('test')->where('id='.$_GET['id'])->getfield('courseid');
    			$map['courseid']=$courseid;
    			$map['no']=Auth::getno();
    			D('screlate')->where($map)->setfield('status','正在测试');
	    		$Model=D('choicet_detail');
				//$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
			   //$p = new Page ( $count, 20 ); 
				$list=$Model->where('testid='.$_GET['id'].' '.'and'.' '.'number='.$_GET['number'])->select();
				if (!empty($list)){
					//$page = $p->show ();
					//$this->assign ( "page", $page );
					$this->assign('number1',$_GET['number']); // 模板发量赋值
					$number=$_GET['number']+1;
					$number2=$_GET['number']-1;
					$this->assign('list',$list); // 模板发量赋值
					$this->assign('testid',$_GET['id']); // 模板发量赋值
					$this->assign('number',$number); // 模板发量赋值
					$this->assign('number2',$number2); // 模板发量赋值
					$this->assign('courseid',$courseid); // 模板发量赋值
					$this->assign('no',Auth::getno()); // 模板发量赋值
					//dump($list);
					$this->display(); // 输出模板
				}
				else 
					//dump($_GET['testid']);
					$this->redirect('mytest_nchoice',array('id'=>$_GET['id'],'number'=>1));
	    	}
    	}
	}
	public function choicetdetail1(){//单选题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$courseid=D('test')->where('id='.$_GET['id'])->getfield('courseid');
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('choice_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$map['courseid']=array('eq',$courseid);
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
		    	$map['departmentid'] = array('eq',Auth::getdepartment());
		    	$map['courseid']=array('eq',$courseid);
				$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('choicerelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['choiceid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function choiceadd(){//单选添加
		if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    	}
    	$this->assign('cnlist',$cnlist);
    	$this->assign('anlist',array('A','B','C','D'));
    	$this->assign('no',Auth::getno()); // 模板发量赋值
		$this->display();
    } 
	public function choicedetail(){//单选题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('choice_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
				$list=$Model->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('choicerelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['choiceid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function choice_add(){//单选题添加
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$choice = D("choice"); // 实例化模型类
			if($choice->Create()){// 创建数据对象
				if($choice->add()){ // 写入数据库
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}
				else{
					$this->error('数据库写入错误',1);
				}
			}else{
				$this->error($choice->getError(),1);
			}

		}
    } 
	public  function choicedelete(){//单选题删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$choice=D("choice");
			$choice->delete($id);
			$this->redirect('choicedetail');
		}
    } 
	public  function choiceedit(){//单选题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
			$cname=D('choice_detail')->where('id='.$_GET['id'])->getfield('cname');
			$list=D('choice')->where('id='.$_GET['id'])->select();
			$this->assign('cnlist',$cnlist);
			$this->assign('list',$list);
			$this->assign('cname',$cname);
            $choice=D("choice"); 
            $vo=$choice->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->assign('anlist',array('A','B','C','D'));
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
	public function choiceupdate() { //单选题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $choice=D("choice"); 
		if($choice->Create()){// 创建数据对象
			$choice->save();
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($choice->getError(),1);
		}
    } 
	public function choicetdetail2(){//单选题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$Model=D('choicet_detail');
			$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
		    $p = new Page ( $count, 20 ); 
			$list=$Model->where('testid='.$_GET['id'])->limit($p->firstRow.','.$p->listRows)->order('number')->select();
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			//$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
    	}
	}
    public function choicet_add()//单选组卷
    {	
    	if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
	    	//dump($_POST['no']);
	    	foreach($_POST['id'] as $k=>$v){    	
	    	$choicerelate = M("choicerelate"); // 实例化模型类
	    	$data['choiceid']=$v;
	    	$data['testid']=$_POST['testid'];
	    	//$data['number']=$k;
	    	//$choicerelate->Create();
	    	//$choicerelate->add();
	    	//dump($choicerelate);
	    	$aa=D("choicerelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'choiceid='.$v)->find();
	    	//$aa=D("choicerelate")->where('testid=2 and choiceid=9')->find();
	    	//dump($aa);
	    	//dump($_POST['id']);
	    	//echo $_POST['testid'];
	    	//echo $v;
	    	if (empty($aa))
		    		$choicerelate->add($data); // 写入数据库	
		    	/*else {
					$choicerelate->add($data); // 写入数据库		
					$this->redirect('saqtdetail2',array('id'=>$_POST['testid']));
					}*/
	    	}
    	}
		$this->redirect('choicetdetail2',array('id'=>$_POST['testid']));
    } 
    public  function choicetedit(){//单选题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			//$cnlist=D('course')->select();
			//$cname=D('choice_detail')->where('id='.$_GET['id'])->getfield('cname');
			//$this->assign('cnlist',$cnlist);
			//$this->assign('cname',$cname);
            $choice=D("choicerelate"); 
            $vo=$choice->getById($_GET['id']); 
            if($vo) { 
            	$this->assign('testid',$_GET['testid']);
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
    public function choicetupdate() { //单选题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $choicerelate=D("choicerelate"); 
        if ($_POST['number']!=$_POST['number1'])
	        $aa=D("choicerelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'number='.$_POST['number'])->find();
	        if ($aa)
	        	$this->error('题号已存在',1);
	        else{
		        if($choicerelate->Create()){// 创建数据对象
					$choicerelate->save();
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}else{
					$this->error($choicerelate->getError(),1);
				}
	        }
    } 
    public  function choicetdelete(){//单选题组卷删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$choice=D("choicerelate");
			$choice->delete($id);
			$this->redirect('choicetdetail2',array('id'=>$_POST['testid']));
		}
    } 
    public function chsave()//单选题答案保存
    {	  	
    	$chsave = M("chsave"); // 实例化模型类
    	$map['testid']=array('eq',$_POST['testid']);
    	$map['choiceid']=array('eq',$_POST['choiceid']);
    	$map['no']=array('eq',$_POST['no']);
    	$aa=D("chsave")->where($map)->find();
    	if (empty($aa)){
	    	$data['choiceid']=$_POST['choiceid'];
	    	$data['testid']=$_POST['testid'];
	    	$data['no']=$_POST['no'];
	    	$data['answer']=$_POST['answer'];
	    	$data['number']=$_POST['number1'];
	    	$data['score1']=$_POST['score1'];
	    	$chsave->Create();
	    	$chsave->add($data);
    	}
    	else 	
    		$chsave->where($map)->setfield('answer',$_POST['answer']);
	    	//$chsave->save();
		$this->redirect('mytest_choice',array('id'=>$_POST['testid'],'number'=>$_POST['number']));
    } 
//多选题
	public function mytest_nchoice(){//多选题_学生
		if(Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	else{
    		$type1=D('test')->where('id='.$_GET['id'])->getfield('type1');
    		if ($type1=='0')
    			exit('测试还未开始!');
    		else {
    			$courseid=D('test')->where('id='.$_GET['id'])->getfield('courseid');
    			$map['courseid']=$courseid;
    			$map['no']=Auth::getno();
    			D('screlate')->where($map)->setfield('status','正在测试');
	    		$Model=D('nchoicet_detail');
				//$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
			   //$p = new Page ( $count, 20 ); 
				$list=$Model->where('testid='.$_GET['id'].' '.'and'.' '.'number='.$_GET['number'])->select();
				if (!empty($list)){
					//$page = $p->show ();
					//$this->assign ( "page", $page );
					$this->assign('number1',$_GET['number']); // 模板发量赋值
					$number=$_GET['number']+1;
					$number2=$_GET['number']-1;
					$this->assign('list',$list); // 模板发量赋值
					$this->assign('testid',$_GET['id']); // 模板发量赋值
					$this->assign('number',$number); // 模板发量赋值
					$this->assign('number2',$number2); // 模板发量赋值
					$this->assign('no',Auth::getno()); // 模板发量赋值
					//dump($list);
					$this->display(); // 输出模板
				}
				else 
					$this->redirect('mytest_blank',array('id'=>$_GET['id'],'number'=>1));
	    	}
    	}
	}
	public function nchoicetdetail1(){//多选题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('nchoice_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
		    	$map['departmentid'] = array('eq',Auth::getdepartment());
				$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('nchoicerelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['nchoiceid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function nchoiceadd(){//多选添加
		if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    	}
    	$this->assign('cnlist',$cnlist);
    	$this->assign('anlist',array('A','B','C','D','AB','AC','AD','BC','BD','CD','ABC','ACD','ABD','BCD','ABCD'));
    	$this->assign('no',Auth::getno()); // 模板发量赋值
		$this->display();
    } 
	public function nchoicedetail(){//多选题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('nchoice_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
				$list=$Model->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('nchoicerelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['nchoiceid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function nchoice_add(){//多选题添加
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$nchoice = D("nchoice"); // 实例化模型类
			if($nchoice->Create()){// 创建数据对象
				if($nchoice->add()){ // 写入数据库
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}
				else{
					$this->error('数据库写入错误',1);
				}
			}else{
				$this->error($nchoice->getError(),1);
			}

		}
    } 
	public  function nchoicedelete(){//多选题删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$nchoice=D("nchoice");
			$nchoice->delete($id);
			$this->redirect('nchoicedetail');
		}
    } 
	public  function nchoiceedit(){//多选题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
			$cname=D('nchoice_detail')->where('id='.$_GET['id'])->getfield('cname');
			$list=D('nchoice')->where('id='.$_GET['id'])->select();
			$this->assign('cnlist',$cnlist);
			$this->assign('list',$list);
			$this->assign('cname',$cname);
            $nchoice=D("nchoice"); 
            $vo=$nchoice->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->assign('anlist',array('A','B','C','D','AB','AC','AD','BC','BD','CD','ABC','ACD','ABD','BCD','ABCD'));
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
	public function nchoiceupdate() { //多选题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $nchoice=D("nchoice"); 
		if($nchoice->Create()){// 创建数据对象
			$nchoice->save();
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($nchoice->getError(),1);
		}
    } 
	public function nchoicetdetail2(){//多选题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$Model=D('nchoicet_detail');
			$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
		    $p = new Page ( $count, 20 ); 
			$list=$Model->where('testid='.$_GET['id'])->limit($p->firstRow.','.$p->listRows)->order('number')->select();
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			//$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
    	}
	}
    public function nchoicet_add()//多选组卷
    {	
    	if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
	    	//dump($_POST['no']);
	    	foreach($_POST['id'] as $k=>$v){    	
	    	$nchoicerelate = M("nchoicerelate"); // 实例化模型类
	    	$data['nchoiceid']=$v;
	    	$data['testid']=$_POST['testid'];
	    	//$nchoicerelate->Create();
	    	//$nchoicerelate->add();
	    	//dump($nchoicerelate);
	    	$aa=D("nchoicerelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'nchoiceid='.$v)->find();
	    	//$aa=D("nchoicerelate")->where('testid=2 and nchoiceid=9')->find();
	    	//dump($aa);
	    	//dump($_POST['id']);
	    	//echo $_POST['testid'];
	    	//echo $v;
	    	if (empty($aa))
		    		$nchoicerelate->add($data); // 写入数据库	
		    	/*else {
					$nchoicerelate->add($data); // 写入数据库		
					$this->redirect('saqtdetail2',array('id'=>$_POST['testid']));
					}*/
	    	}
    	}
		$this->redirect('nchoicetdetail2',array('id'=>$_POST['testid']));
    } 
    public  function nchoicetedit(){//多选题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			//$cnlist=D('course')->select();
			//$cname=D('nchoice_detail')->where('id='.$_GET['id'])->getfield('cname');
			//$this->assign('cnlist',$cnlist);
			//$this->assign('cname',$cname);
            $nchoice=D("nchoicerelate"); 
            $vo=$nchoice->getById($_GET['id']); 
            if($vo) { 
            	$this->assign('testid',$_GET['testid']);
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
    public function nchoicetupdate() { //多选题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $nchoicerelate=D("nchoicerelate"); 
        if ($_POST['number']!=$_POST['number1'])
	        $aa=D("nchoicerelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'number='.$_POST['number'])->find();
	        if ($aa)
	        	$this->error('题号已存在!',1);
	        else{
		        if($nchoicerelate->Create()){// 创建数据对象
					$nchoicerelate->save();
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}else{
					$this->error($nchoicerelate->getError(),1);
				}
	        }
    } 
    public  function nchoicetdelete(){//多选题组卷删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$nchoice=D("nchoicerelate");
			$nchoice->delete($id);
			$this->redirect('nchoicetdetail2',array('id'=>$_POST['testid']));
		}
    } 
    public function nchsave()//多选题答案保存
    {	  	
    	$nchsave = M("nchsave"); // 实例化模型类
    	foreach($_POST['answer'] as $k=>$v){
    		$answer=$answer.$v;
    	}
    	$map['testid']=array('eq',$_POST['testid']);
    	$map['nchoiceid']=array('eq',$_POST['nchoiceid']);
    	$map['no']=array('eq',$_POST['no']);
    	$aa=D("nchsave")->where($map)->find();
    	if (empty($aa)){
	    	$data['nchoiceid'] = $_POST['nchoiceid'];
	    	$data['testid'] = $_POST['testid'];
	    	$data['no'] = $_POST['no'];
	    	$data['answer'] = $answer;
	    	$data['number'] = $_POST['number1'];
	    	$data['score1']=$_POST['score1'];
	    	$nchsave->Create();
	    	$nchsave->add($data);
    	}
    	else 	
    		$nchsave->where($map)->setfield('answer',$answer);
	    	//dump($answer);
		$this->redirect('mytest_nchoice',array('id'=>$_POST['testid'],'number'=>$_POST['number']));
    } 
    //填空题
 	public function blank_save()//评分
    {	  	
    	$score3=D('blanksave')->where('id='.$_POST['id'])->getfield('score1');
   		if (Auth::getrole()=='学生'||Auth::getrole()=='访客')
			$this->redirect('auth/logon');		
		elseif ($_POST['score']<=$score3&&$_POST['score']>0){
			//$form=D('blanksave');
			$score2=D('blanksave')->where('id='.$_POST['id'])->getfield('score');
			D('blanksave')->where('id='.$_POST['id'])->setfield('score',$_POST['score']);
			$aa=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->find();
			if (empty($aa)){
		    	$form=D('score');
		    	$data['score']=$_POST['score'];
		    	$data['no']=$_POST['no'];
		    	$data['testid']=$_POST['testid'];
		    	$form->create();
		    	$form->add($data);
		    	$this->ajaxReturn(0,'表单数据保存成功！',1);
			}
			else {
				$score1=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('score');
				$id=D('score')->where('testid='.$_POST['testid'].' '.'and'.' '.'no='.$_POST['no'])->getfield('id');	
				$score=$_POST['score']+$score1-$score2;
				D('score')->where('id='.$id)->setfield('score',$score);	
				$this->ajaxReturn(0,'表单数据保存成功！',1);
			}
			//$_POST['number']=$_POST['number']+1;
			//dump($score2);dump($score1);dump($_POST['score']);dump($_POST['id']);
			//$this->redirect('correcttest3',array('courseid'=>$_POST['courseid'],'testid'=>$_POST['testid'],'no'=>$_POST['no'],'number'=>$_POST['number']));
		}
		else 
			$this->error('输入分值有误!',1);
    } 
	public function mytest_blank(){//填空题_学生
		if(Auth::getrole()=='访客')
			$this->redirect('auth/logon');
    	else{
    		$type1=D('test')->where('id='.$_GET['id'])->getfield('type1');
    		if ($type1=='0')
    			exit('测试还未开始!');
    		else {
    			$courseid=D('test')->where('id='.$_GET['id'])->getfield('courseid');
    			$map['courseid']=$courseid;
    			$map['no']=Auth::getno();
    			D('screlate')->where($map)->setfield('status','正在测试');
	    		$Model=D('blankt_detail');
				//$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
			   //$p = new Page ( $count, 20 ); 
				$list=$Model->where('testid='.$_GET['id'].' '.'and'.' '.'number='.$_GET['number'])->select();
				if (!empty($list)){
					//$page = $p->show ();
					//$this->assign ( "page", $page );
					$this->assign('number1',$_GET['number']); // 模板发量赋值
					$number=$_GET['number']+1;
					$number2=$_GET['number']-1;
					$this->assign('list',$list); // 模板发量赋值
					$this->assign('testid',$_GET['id']); // 模板发量赋值
					$this->assign('number',$number); // 模板发量赋值
					$this->assign('number2',$number2); // 模板发量赋值
					$this->assign('no',Auth::getno()); // 模板发量赋值
					//dump($list);
					$this->display(); // 输出模板
				}
				else 
					$this->redirect('mytest_saq',array('id'=>$_GET['id'],'number'=>1));
    		}
    	}
	}
	public function blanktdetail1(){//填空题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('blank_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
		    	$map['departmentid'] = array('eq',Auth::getdepartment());
				$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('blankrelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['blankid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function blankadd(){//填空添加
		if(Auth::getrole()=='学生'||Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    	}
    	$this->assign('cnlist',$cnlist);
    	$this->assign('no',Auth::getno()); // 模板发量赋值
		$this->display();
    } 
	public function blankdetail(){//填空题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
    		//$tlist=D('test')->where()->select();
    		$Model=D('blank_detail');
    		if(!empty($_GET['name'])||($_GET['nname'])||($_GET['cname'])){
				$map['nname'] = array('like','%'.$_GET['nname'].'%');
				$map['name'] = array('like','%'.$_GET['name'].'%');
				if ($_GET['cname'])
				$map['cname'] = array('eq',$_GET['cname']);
				$map['departmentid'] = array('eq',Auth::getdepartment());
				$count = $Model->where($map)->count();    //计算总数 
		        $p = new Page ( $count, 50 ); 
		    	$list=$Model->where($map)->limit($p->firstRow.','.$p->listRows)->order('id')->select();
	    	}
			else {
				$count = $Model->count();    //计算总数 
		    	$p = new Page ( $count, 20 ); 
				$list=$Model->limit($p->firstRow.','.$p->listRows)->order('id')->select();
			}
    	foreach($list as $k=>$v){
			$Form = D('blankrelate');
			$map1['testid']=array('eq',$_GET['id']);
			$map1['blankid']=array('eq',$v['id']);
			if($Form->where($map1)->select())
				$list[$k]["isselect"]=true;
			else
				$list[$k]["isselect"]=false;
			}
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('cnlist',$cnlist); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
	}
	}
	public function blank_add(){//填空题添加
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$blank = D("blank"); // 实例化模型类
			if($blank->Create()){// 创建数据对象
				if($blank->add()){ // 写入数据库
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}
				else{
					$this->error('数据库写入错误',1);
				}
			}else{
				$this->error($blank->getError(),1);
			}

		}
    } 
	public  function blankdelete(){//填空题删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$blank=D("blank");
			$blank->delete($id);
			$this->redirect('blankdetail');
		}
    } 
	public  function blankedit(){//填空题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			$cnlist=D('course')->where('departmentid='.Auth::getdepartment())->select();
			$cname=D('blank_detail')->where('id='.$_GET['id'])->getfield('cname');
			$this->assign('cnlist',$cnlist);
			$this->assign('cname',$cname);
            $blank=D("blank"); 
            $vo=$blank->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
	public function blankupdate() { //填空题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $blank=D("blank"); 
		if($blank->Create()){// 创建数据对象
			$blank->save();
			$this->ajaxReturn(0,'表单数据保存成功！',1);
		}else{
			$this->error($blank->getError(),1);
		}
    } 
	public function blanktdetail2(){//填空题
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
    		$Model=D('blankt_detail');
			$count = $Model->where('testid='.$_GET['id'])->count();    //计算总数 
		    $p = new Page ( $count, 20 ); 
			$list=$Model->where('testid='.$_GET['id'])->limit($p->firstRow.','.$p->listRows)->order('number')->select();
			$page = $p->show ();
			$this->assign ( "page", $page );
			$this->assign('list',$list); // 模板发量赋值
			$this->assign('testid',$_GET['id']); // 模板发量赋值
			//$this->assign('no',Auth::getno()); // 模板发量赋值
			$this->display(); // 输出模板
    	}
	}
    public function blankt_add()//填空组卷
    {	
    	if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
    	else{
	    	//dump($_POST['no']);
	    	foreach($_POST['id'] as $k=>$v){    	
		    	$blankrelate = M("blankrelate"); // 实例化模型类
		    	$data['blankid']=$v;
		    	$data['testid']=$_POST['testid'];
		    	//$blankrelate->Create();
		    	//$blankrelate->add();
		    	//dump($blankrelate);
		    	$aa=D("blankrelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'blankid='.$v)->find();
		    	//$aa=D("blankrelate")->where('testid=2 and blankid=9')->find();
		    	//dump($aa);
		    	//dump($_POST['id']);
		    	//echo $_POST['testid'];
		    	//echo $v;
		    	if (empty($aa))
		    		$blankrelate->add($data); // 写入数据库	
		    	/*else {
					$blankrelate->add($data); // 写入数据库		
					$this->redirect('blanktdetail2',array('id'=>$_POST['testid']));
					}*/
	    	}
    	}
		$this->redirect('blanktdetail2',array('id'=>$_POST['testid']));
    } 
    public  function blanktedit(){//填空题编辑
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
		if(!empty($_GET['id'])) { 
			//$cnlist=D('course')->select();
			//$cname=D('blank_detail')->where('id='.$_GET['id'])->getfield('cname');
			//$this->assign('cnlist',$cnlist);
			//$this->assign('cname',$cname);
            $blank=D("blankrelate"); 
            $vo=$blank->getById($_GET['id']); 
            if($vo) { 
            	$this->assign('testid',$_GET['testid']);
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
		}
    } 
    public function blanktupdate() { //填空题更新
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
    		$this->redirect('auth/logon');
        $blankrelate=D("blankrelate"); 
        if ($_POST['number']!=$_POST['number1'])
	        $aa=D("blankrelate")->where('testid='.$_POST['testid'].' '.'and'.' '.'number='.$_POST['number'])->find();
	        if ($aa)
	        	$this->error('题号已存在',1);
	        else{
		        if($blankrelate->Create()){// 创建数据对象
					$blankrelate->save();
					$this->ajaxReturn(0,'表单数据保存成功！',1);
				}else{
					$this->error($blankrelate->getError(),1);
				}
	        }
    } 
    public  function blanktdelete(){//填空题组卷删除
		if(Auth::getrole()=='访客'||Auth::getrole()=='学生')
			$this->redirect('auth/logon');
		else{
			$id=$_POST['id'];
			$blank=D("blankrelate");
			$blank->delete($id);
			$this->redirect('blanktdetail2',array('id'=>$_POST['testid']));
		}
    } 
    public function blanksave()//填空题答案保存
    {	  	
    	$blanksave = M("blanksave"); // 实例化模型类
    	$map['testid']=array('eq',$_POST['testid']);
    	$map['blankid']=array('eq',$_POST['blankid']);
    	$map['no']=array('eq',$_POST['no']);
    	$aa=D("blanksave")->where($map)->find();
    	if (empty($aa)){
	    	$data['blankid']=$_POST['blankid'];
	    	$data['testid']=$_POST['testid'];
	    	$data['no']=$_POST['no'];
	    	$data['answer']=$_POST['answer'];
	    	$data['number']=$_POST['number1'];
	    	$data['score1']=$_POST['score1'];
	    	$blanksave->Create();
	    	$blanksave->add($data);
    	}
    	else 	
    		$blanksave->where($map)->setfield('answer',$_POST['answer']);
	    	//$blanksave->save();
		$this->redirect('mytest_blank',array('id'=>$_POST['testid'],'number'=>$_POST['number']));
    } 
    public function myscore()//学生查看得分
    {
    	if(Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		else{
    	$list=D('score_detail')->where('no='.Auth::getno())->select();
    	$this->assign('list',$list);
    	$this->display();
		}
    }
	public function workscore()//查看作业得分
    {
    	if(Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		else{
    	$list=D('work_detail')->where('saveid='.$_GET['p'])->order('no')->select();
    	$this->assign('list',$list);
    	$this->display();
		}
    }
	public function testscore()//查看测试得分
    {
    	if(Auth::getrole()=='访客')
			$this->redirect('auth/logon');
		else{
    	$list=D('score_detail')->where('testid='.$_GET['id'])->select();
    	$this->assign('list',$list);
    	$this->display();
		}
    }
	public function mytestend()//试卷提交
    {
    	if(Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	$this->assign('courseid',$_GET['courseid']);
    	$this->assign('testid',$_GET['testid']);
    	$this->assign('no',$_GET['no']);
    	$this->display();
    }
    public function mytest_end()//试卷提交
    {
    	if(Auth::getrole()=='访客')
    		$this->redirect('auth/logon');
    	$map['courseid']=$_GET['courseid'];
    	$map['no']=$_GET['no'];
    	D('screlate')->where($map)->setfield('status','已提交');
    	$this->redirect('mytest');
    }
}
?>